package com.css.fxfzfxqh.modules.buildings.calculationTasks.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.CalculationTasks;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.service.CalculationTasksService;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.entity.ResistEarthquakesDE;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.param.AssessParam;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import com.css.fxfzfxqh.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/09/20
 * @Description: 建筑物计算任务
 */
@Slf4j
@RestController
@RequestMapping("/calculationTasks")
public class CalculationTasksController {

    @Resource
    private CalculationTasksService calculationTasks;

    /**
     * 新增任务
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/addTask")
    public RestResponse addTask(@RequestBody CalculationTasks param) throws Exception {
        String mess=calculationTasks.addTask(param);
        return RestResponse.succeed(mess);
    }

    //获取进度条百分比
    @GetMapping("/getProgress")
    public RestResponse getProgress(){
        String num = calculationTasks.getProgress();
        return RestResponse.succeed(num);
    }

    //取消复制
    @GetMapping("/cancelCopy")
    public RestResponse cancelCopy(){
        calculationTasks.cancelCopy();
        return RestResponse.succeed("取消成功");
    }

    //取消复制
    @GetMapping("/findById")
    public RestResponse findById(String id){
        CalculationTasks tasks=calculationTasks.findById(id);
        return RestResponse.succeed(tasks);
    }
    //查询当前任务
    @GetMapping("/getTasks")
    public RestResponse getTasks(){
        CalculationTasks tasks=calculationTasks.getTasks();
        return RestResponse.succeed(tasks);
    }

    //设置为当前任务
    @GetMapping("/settingTasks")
    public RestResponse settingTasks(String id){
        String s = calculationTasks.settingTasks(id);
        if (s.equals("非本人创建数据不可修改")){
            return RestResponse.fail(s);
        }
        return RestResponse.succeed();
    }
    //设置为当前任务
    @GetMapping("/deleteById")
    public RestResponse deleteById(String id){
        calculationTasks.deleteById(id);
        return RestResponse.succeed();
    }

    /**
     * 分页列表
     *
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, CalculationTasks param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = calculationTasks.getPage(curPage, pageSize, param);
            if ("无权限".equals(page.get("Message"))){
                return RestResponse.fail("无权限");
            }
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取当前登录人角色
     * @return
     */
    @GetMapping("/getRole")
    public RestResponse getRole() {
        RestResponse response = null;
        try{
            Map<String, String> map= calculationTasks.getRole();
            response = RestResponse.succeed(map);
            response.setMessage("查询成功");
        }catch (Exception e){
            log.error("查询失败!",e);
            response=RestResponse.fail("查询失败!");
        }
        return response;
    }

    /**
     * 修改任务
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/upDateTasks")
    public RestResponse upDateAssess(@RequestBody CalculationTasks param) {
        try {
            calculationTasks.upDateTasks(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改评估失败!", e);
            return RestResponse.fail("修改失败!");
        }
    }

    /**
     * @desc 任务管理概率评估获取评估年份
     * @param param
     * @return null
     * @desc
     */
    @PostMapping("/getYear")
    public RestResponse getYear(@RequestBody  CalculationTasks param) throws Exception {
        return ResponseCreator.createSuccessResponse(calculationTasks.getYear(param));
    }
}
